package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.StringUtil;
import com.qire.common.collect.ArrayUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.manhua.R;
import com.qire.manhua.dal.IComicDataDAL;
import com.qire.manhua.model.viewModel.impl.ComicRankListOfVipViewModel;
import com.qire.manhua.model.vo.ComicRankListOfVipVO;
import com.qire.manhua.view.attributeBinder.attribute.ImageAttribute;

public class ComicRankListOfVipService extends DataService {

    private final IComicDataDAL comicDataDAL = WarehouseFactory.create(this, IComicDataDAL.class);
    private final ComicRankListOfVipViewModel viewModel;

    private final String[] rankTypeArray = {"week", "month", "total"};
    private int tabIndex = 0;

    public ComicRankListOfVipService(ComicRankListOfVipViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**
     * 刷新 {@code tabIndex} 指向类型数据，并移动下一个直到遍历排行榜类型最后一个。
     */
    public void loadNextTabData() {
        if(tabIndex < rankTypeArray.length) {
            viewModel.setCurrentTabIndex(tabIndex);
            fetchComicRankList(rankTypeArray[tabIndex++]);
        }
    }

    /**
     * 获取排行榜数据根据排行榜类型
     * @param rankType 排行榜类型值域 {"week","month","total"}
     */
    public void fetchComicRankList(String rankType) {
        comicDataDAL.fetchComicRankListOfVip(rankType);
    }
    @StrategyAnnotation(event = Url.v4_vipRanking)
    public void fetchComicRankListOfVipCallBack(ComicRankListOfVipVO[] dataVO, boolean isCache) {
        // 刷新排行榜列表数据，如果有数据。
        if(ArrayUtil.notEmpty(dataVO)) {
            int serialNumber = 1;
            for(ComicRankListOfVipVO vo : dataVO) {
                int          comicId            = vo.id;
                ImageAttribute comicCover       = new ImageAttribute(vo.book_unruly, R.drawable.app_placeholder220x290);
                CharSequence floatLayerText     = String.format("更新至%s话", vo.chapter_px);
                CharSequence comicName          = vo.book_name;
                CharSequence comicAuthor        = String.format("作者: %s", vo.book_author);
                CharSequence comicBrief         = vo.short_title;
                CharSequence comicLabel         = vo.comicLabel();
                CharSequence comicPopularity    = String.format("人气值: %s", StringUtil.toSymbol(vo.read_cnt));
                int          comicSerialNumber  = serialNumber++;
                viewModel.addRankItemViewModel(comicId, comicCover, floatLayerText, comicName, comicAuthor, comicBrief, comicLabel, comicPopularity, comicSerialNumber);
            }
            viewModel.refreshRankUI();
        }
        // 加载下一个
        loadNextTabData();
    }
}
